package com.xiaoshuidi.cloud.module.rooms.api;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.tenant.core.util.TenantUtils;
import com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.finance.api.finance.FinancePaymentApi;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.FinancePaymentDTO;
import com.xiaoshuidi.cloud.module.finance.enums.finance.DirectionEnum;
import com.xiaoshuidi.cloud.module.rooms.api.apartment.dto.ApartmentListDto;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.ReserveGetResp;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseReservePaymentStatus;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseReserveStatus;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.service.ApartmentService;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomHouseReserveService;
import com.xiaoshuidi.cloud.module.rooms.service.RoomHouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class FinanceApi implements FinancePaymentApi {
    private final IRoomHouseReserveService reserveService;
    private final RoomHouseService roomHouseService;
    private final ApartmentService apartmentService;

    @Override
    public CommonResult<FinancePaymentDTO> createPayment(String sourceBizId, String sourceBizType) {
        FinancePaymentDTO.FinancePaymentDTOBuilder builder = FinancePaymentDTO.builder();
        return TenantUtils.executeIgnoreAndReturn(() -> {
            ReserveGetResp reserve = reserveService.get(Long.valueOf(sourceBizId));
            if (reserve == null) {
                return CommonResult.error(ErrorCodeConstants.ROOM_ORDER_NOT_EXISTS);
            }
            RoomHouse house = roomHouseService.getById(reserve.getRoomHouseId());
            String houseName = "";
            if (house.getIsConcentrated()) {
                houseName = String.format("%s%s栋%s", house.getArea(), house.getBuildingNo(), house.getName());
            } else {
                houseName = String.format("%s%s栋%s单元%s", house.getArea(), house.getBuildingNo(), house.getUnitNo(), house.getName());
            }
            ApartmentListDto apartment = apartmentService.getById(house.getApartmentId());
            if (RoomHouseReserveStatus.TO_BE_PAID.getCode().equals(reserve.getStatus())){
                if (RoomHouseReservePaymentStatus.PROCESSED.getCode().equals(reserve.getPaymentStatus())) {
                    return CommonResult.error(ErrorCodeConstants.ROOM_ORDER_ALREADY_PAID);
                } else if (RoomHouseReservePaymentStatus.CANCELED.getCode().equals(reserve.getPaymentStatus())) {
                    return CommonResult.error(com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants.PAY_ORDER_CANCELLED);
                }
                return CommonResult.success(builder.subject("预定账单支付")
                        .renter(reserve.getBooker())
                        .rentCustomer(reserve.getBooker())
                        .roomId(reserve.getRoomHouseId())
                        .roomName(houseName)
                        .apartmentId(house.getApartmentId())
                        .apartmentName(apartment.getName())
                        .direction(DirectionEnum.IN.getValue())
                        .tradeAmount(reserve.getDeposit())
                        .memberId(reserve.getBookerId()).build());
            }else if (RoomHouseReserveStatus.RETIRED.getCode().equals(reserve.getStatus())){
                return CommonResult.success(builder.subject("退定账单支付")
                        .renter(reserve.getBooker())
                        .rentCustomer(reserve.getBooker())
                        .roomId(reserve.getRoomHouseId())
                        .roomName(houseName)
                        .apartmentId(house.getApartmentId())
                        .apartmentName(apartment.getName())
                        .direction(DirectionEnum.OUT.getValue())
                        .tradeAmount(reserve.getWithdrawAmount())
                        .memberId(reserve.getBookerId()).build());
            }else {
                return CommonResult.error(ErrorCodeConstants.ROOM_ORDER_NOT_EXISTS);
            }
        });
    }
}